package net.uhuijia.zb.userzhubolivegood.service.impl;

import net.uhuijia.constants.DBConst;
import net.uhuijia.enums.ErrorCodeEnum;
import net.uhuijia.exception.BusinessException;
import net.uhuijia.goods.goodspu.clientservice.GoodSpuClientService;
import net.uhuijia.user.usermall.clientservice.UserMallClientService;
import net.uhuijia.vo.good.GoodZBListVo;
import net.uhuijia.vo.zb.OnlineLiveGoodVo;
import net.uhuijia.zb.userzhubolive.persistence.model.UserZhuboLive;
import net.uhuijia.zb.userzhubolive.service.UserZhuboLiveService;
import net.uhuijia.zb.userzhubolivegood.service.UserZhuboLiveGoodService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import net.uhuijia.appbase.service.CrudService;
import net.uhuijia.zb.userzhubolivegood.persistence.dao.UserZhuboLiveGoodDao;
import net.uhuijia.zb.userzhubolivegood.persistence.model.UserZhuboLiveGood;
import net.uhuijia.zb.userzhubolivegood.persistence.model.UserZhuboLiveGoodExample;
import net.uhuijia.zb.userzhubolivegood.persistence.model.UserZhuboLiveGoodExample.Criteria;

import java.util.ArrayList;
import java.util.List;


@Service
@Transactional
public class UserZhuboLiveGoodServiceImpl
     extends CrudService<UserZhuboLiveGoodDao,UserZhuboLiveGood,UserZhuboLiveGoodExample> implements UserZhuboLiveGoodService {
    @Autowired
    private UserZhuboLiveGoodDao userZhuboLiveGoodDao;
    @Autowired
    private UserZhuboLiveService userZhuboLiveService;
    @Autowired
    private GoodSpuClientService goodSpuClientService;

    /**
     * 直播间右侧商品列表
     * @param liveId
     * @param example
     * @return
     */
    @Override
    public List<GoodZBListVo> onlineLiveGoodList(String liveId, UserZhuboLiveGoodExample example) {
        UserZhuboLive userZhuboLive =  this.userZhuboLiveService.get(liveId);
        if(userZhuboLive!=null){
            //分页查询主播直播的商品
            List<UserZhuboLiveGood> userZhuboLiveGoods = this.findUserZhuboLiveGoodByLiveIdAndPage(liveId,example);
            List<GoodZBListVo> goodZBListVos = new ArrayList<>();
            for(UserZhuboLiveGood userZhuboLiveGood : userZhuboLiveGoods){
                GoodZBListVo goodZBListVo = this.userZhuboLiveGoodWrapToOnlineLiveGoodVo(userZhuboLiveGood);
                goodZBListVos.add(goodZBListVo);
            }
            return goodZBListVos;
        }else{
            //提示主播不在线
            throw new BusinessException(ErrorCodeEnum.zb_not_online);
        }
    }
    /**
     * 分页查询主播正在直播的商品列表
     * @param liveId  直播Id
     * @param example 分页的example
     * @return
     */
    @Override
    public List<UserZhuboLiveGood> findUserZhuboLiveGoodByLiveIdAndPage(String liveId,UserZhuboLiveGoodExample example){
        Criteria criteria = example.createCriteria();
        criteria.andIsEffectiveEqualTo(DBConst.TRUE);
        criteria.andLiveIdEqualTo(liveId);
        example.setOrderByClause(" reorder asc");
        return this.list(example);
    }

    /**
     * 将UserZhuboLiveGood 转换成 OnlineLiveGoodVo
     * @param userZhuboLiveGood
     * @return
     */
    private  GoodZBListVo userZhuboLiveGoodWrapToOnlineLiveGoodVo(UserZhuboLiveGood userZhuboLiveGood){
        // 商品转换 feign 需要进行商品沟通
        String spuId = userZhuboLiveGood.getGoodId();
        GoodZBListVo goodZBListVo = this.goodSpuClientService.goodSpuWrapToGoodZBListVo(spuId);
        return goodZBListVo;
    }

    /**
     * 查询直播间商品件数
     * @param liveId
     * @return
     */
    @Override
    public int findUserZhuboLiveGoodCountByLiveIdAndPage(String liveId) {
        UserZhuboLiveGoodExample example = new UserZhuboLiveGoodExample();
        Criteria criteria = example.createCriteria();
        criteria.andIsEffectiveEqualTo(DBConst.TRUE);
        criteria.andLiveIdEqualTo(liveId);
        return this.count(example);

    }
}